/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.LetterDao;
import com.hboxs.asl.entity.Letter;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.LetterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service - 私信
 */
@Service("letterServiceImpl")
public class LetterServiceImpl extends BaseServiceImpl<Letter, Long> implements LetterService {

    @Resource(name = "letterDaoImpl")
    private LetterDao letterDao;

    @Resource(name = "letterDaoImpl")
    public void setBaseDao(LetterDao letterDao) {
        super.setBaseDao(letterDao);
    }

    /**
     * 保存私信
     * @param sender
     * @param receiver
     * @param content
     */
    @Override
    public void generateLetter(Member sender, Member receiver, String content) {
        Letter letter = new Letter();
        letter.setSender(sender);
        letter.setReceiver(receiver);
        letter.setContent(content);
        letter.setReceiverRead(false);
        this.save(letter);
    }

    /**
     * 回复私信
     * @param sender
     * @param receiver
     * @param content
     * @param letterId
     */
    @Override
    public void replyLetter(Member sender, Member receiver, String content, Long letterId) {
        Letter orignLetter = this.find(letterId);
        Letter letter = new Letter();
        letter.setContent(content);
        letter.setSender(sender);
        letter.setReceiver(receiver);
        letter.setForLetter(orignLetter);
        letter.setReceiverRead(false);
        this.save(letter);
    }

    @Override
    public List<Letter> findLetterPage(Member member) {
        return letterDao.findLetterPage(member);
    }

    @Override
    public List<Letter> findLetterDetailList(Letter letter) {
        return letterDao.findLetterDetailList(letter);
    }

    @Override
    public Page<Letter> findPageLetter(Pageable pageable, List<Letter> letterList) {
        // 计算分页
        int total = letterList.size(); // 总数
        int from = (pageable.getPageNumber() - 1) * pageable.getPageSize(); // 分页开始
        int to = pageable.getPageNumber() * pageable.getPageSize(); // 分页结束
        if (from > total) {
            // 超过分页数默认返回第一页
            from = 0;
            to = pageable.getPageSize();
        }
        if (to > total) {
            to = total;
        }
        List<Letter> content = letterList.subList(from, to);
        return new Page<>(content, total, pageable);
    }

    @Override
    public Page<Letter> findPageLetters(Pageable pageable) {
        return letterDao.findPageLetters(pageable);
    }

    @Override
    public Page<Letter> findPageLetterByTime(Pageable pageable, Letter letter) {
        return letterDao.findPageLetterByTime(pageable, letter);
    }

    @Override
    public Letter ifHasSend(Member sender, Member receiver) {
        return letterDao.ifHasSend(sender, receiver);
    }

    /**
     * 获取未读私信数量
     * @param receiver
     * @param letterId
     * @return
     */
    @Override
    public long hasNotReadLetters(Member receiver, Long letterId) {
        return letterDao.hasNotReadLetters(receiver, letterId);
    }


}
